const {
  WORD_ORDER_OPERATION_TYPE
} = require('../../../constant/index')

const {
  updateWorkOrder
} = require('../../../api/workOrder')

const {
  to,
  openLoading,
  closeLoading,
  showToast,
  backParentLoad,
  getEventData
} = getApp()
Page({

  data: {
    operationOptions: [{
        name: WORD_ORDER_OPERATION_TYPE.WORK_ORDER_CUSTOM_ERR_CONTROL,
        text: '客户误操作'
      },
      {
        name: WORD_ORDER_OPERATION_TYPE.WORK_ORDER_NOT_NEED_SERVICE,
        text: '暂时不需要服务'
      },
    ],
    formData: {
      operationType: WORD_ORDER_OPERATION_TYPE.WORK_ORDER_CUSTOM_ERR_CONTROL
    }
  },

  onLoad() {
    this.setData({
      orderData: JSON.parse(this.options.data)
    })
  },

  onCustomTypeChange(event) {
    const {
      type
    } = getEventData(event);

    this.setData({
      [`formData.${type}`]: event.detail
    })
  },

  checkValidate() {
    return this.data.formData;
  },

  handleSubmit() {
    const result = this.checkValidate();

    if (!result) {
      return;
    }
    const {
      operationType
    } = result;
    const params = {
      operationType: operationType,
      operationContent: result.operationContent,
      workOrderId: this.data.orderData.workOrderId,
    }

    this.submitPromise(params);
  },

  async submitPromise(params) {
    openLoading('提交中')
    const [err] = await to(updateWorkOrder(params));
    closeLoading()
    if (!err) {
      showToast({
        message: '提交成功',
        type: 'success',
        mask: true,
        onClose: () => {
          setTimeout(() => {
            backParentLoad(1)
          }, 1000)
        }
      })
    }
  }
})